package com.fisherman.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.annotation.Profile;

import java.util.Arrays;

/**
 * @User: fisherman
 * @Date: 2020/5/23 11:28
 *
 * @Aspect:告诉spring当前类是一个切面类
 *
 */
@Aspect
public class LogAspects {

    //抽取公共的切入点表达式
    //1.本类引用
    //2.其他的切面
    @Pointcut("execution(public int com.fisherman.aop.MathCalculator.*(..))")
    public void pointCut(){

    }

    //@Before在目标方法之前切入，切入点表达式（指定在哪个方法切入）
    @Before("pointCut()")
    public void logStart(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        System.out.println(""+joinPoint.getSignature().getName()+"pointCut()运行。。。参数列表是{"+Arrays.asList(args)+"}");
    }

    @After("com.fisherman.aop.LogAspects.pointCut()")
    public void logEnd(JoinPoint joinPoint){
        System.out.println(""+joinPoint.getSignature().getName()+"除法结束。。。");
    }


    //JoinPoint这个参数一定出现在参数表第一位
    @AfterReturning(value = "pointCut()",returning="result")
    public void logReturn(JoinPoint joinPoint,Object result){
        System.out.println(""+joinPoint.getSignature().getName()+"除法正常返回。。。运行结果是：{"+result+"}");
    }

    @AfterThrowing(value = "pointCut()",throwing = "exception")
    public void logException(JoinPoint joinPoint,Exception exception){
        System.out.println(""+joinPoint.getSignature().getName()+"除法异常。。。异常信息：{"+exception+"}");
    }
}
